package com.sinosoft.weixin.service.impl;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.sinosoft.weixin.service.MakePDFService;
import com.sinosoft.weixin.service.ServiceException;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by huzhengying on 2017/12/22.
 */
@Service
public class MakePDFServiceImpl implements MakePDFService{
    @Override
    public void outputPdf() {
        PdfReader pdfReader = null;
        ByteArrayOutputStream bos = null;
        try{
            pdfReader = new PdfReader(MakePDFService.class.getResourceAsStream("ElectronicPlanBook.pdf"));
            bos = new ByteArrayOutputStream();
            PdfStamper ps = new PdfStamper(pdfReader, bos);

            BaseFont bf = BaseFont.createFont(
                    "STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
            ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
            fontList.add(bf);

            AcroFields s = ps.getAcroFields();
            s.setSubstitutionFonts(fontList);
            s.setFieldProperty("Text1","textfont",bf,null);
            s.setFieldProperty("Text1","textsize",30,null);
            s.setField("Text1","这是一波终极测试打卡看大口大口大口大打卡打卡da");

            ps.setFormFlattening(true);
            ps.close();

            FileOutputStream fos = new FileOutputStream("/Users/huzhengying/Desktop/test.pdf");
            fos.write(bos.toByteArray());
            fos.close();
        }catch (Exception ex){
            throw new ServiceException("生成失败",ex);
        }finally {
            IOUtils.closeQuietly(bos);

        }
    }

    public void test(){
        PdfReader pdfReader = null;
        ByteArrayOutputStream bos = null;
        try{

        }catch (Exception ex){
            throw new ServiceException("生成失败",ex);
        }
    }

    public void test1() throws Exception{
        //创建一个pdf读入流
        PdfReader reader = new PdfReader("/Users/huzhengying/Desktop/ElectronicPlanBook.pdf");
        //根据一个pdfreader创建一个pdfStamper.用来生成新的pdf
        PdfStamper stamper = new PdfStamper(
                reader, new FileOutputStream("/Users/huzhengying/Desktop/test.pdf"));
        BaseFont baseFont = BaseFont.createFont(
                        "/Users/huzhengying/Desktop/字体/simsun_0.ttf",
                        BaseFont.IDENTITY_H,
                        BaseFont.NOT_EMBEDDED);
        //baseFont不支持字体样式设定.但是font字体要求操作系统支持此字体会带来移植问题.
        Font font = new Font(baseFont,22,Font.BOLDITALIC);
        font.setStyle(Font.BOLD);
        font.getBaseFont();

        PdfContentByte over = stamper.getOverContent(1);
        //开始写入文本
        over.beginText();
        over.setFontAndSize(font.getBaseFont(), 22);
        over.setColorFill(BaseColor.BLACK);
        over.setTextMatrix(107, 630);
        over.showText("南京龙腾机械设备制造有限公司");
        over.endText();

        over.beginText();
        over.setFontAndSize(font.getBaseFont(), 22);
        over.setColorFill(BaseColor.BLACK);
        over.setTextMatrix(160, 590);
        over.showText("员工福利保障计划");
        over.endText();

        for (int i=1; i<=reader.getNumberOfPages(); i++){
            //获得pdfstamper在当前页的上层打印内容.也就是说 这些内容会覆盖在原先的pdf内容之上.
//            PdfContentByte over = stamper.getOverContent(i);
//            //用pdfreader获得当前页字典对象.包含了该页的一些数据.比如该页的坐标轴信息.
//            PdfDictionary p = reader.getPageN(i);
//            //拿到mediaBox 里面放着该页pdf的大小信息.
//            PdfObject po =  p.get(new PdfName("MediaBox"));
//            //po是一个数组对象.里面包含了该页pdf的坐标轴范围.
//            PdfArray pa = (PdfArray) po;
//            System.out.println(pa.size());
//            //看看y轴的最大值.
//            System.out.println(pa.getAsNumber(pa.size()-1));
//            //开始写入文本
//            over.beginText();
//            //设置字体和大小
//            over.setFontAndSize(font.getBaseFont(), 10);
//            //设置字体颜色
//            over.setColorFill(BaseColor.RED);
//            //设置字体的输出位置
//            over.setTextMatrix(107, 540);
//            //要输出的text
//            over.showText("我要加[终稿]字样 " + i);
//            over.endText();
//
//            //画一个圈.
//            over.setRGBColorStroke(0xFF, 0x00, 0x00);
//            over.setLineWidth(5f);
//            over.ellipse(250, 450, 350, 550);
//            over.stroke();
        }

        stamper.close();
    }

}
